﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4_06_文件夹的复制和跨盘移动
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!(Directory.Exists(textBox1.Text) && Directory.Exists(this.textBox2.Text)))
            {
                MessageBox.Show("文件夹不存在");
                return;
            }
            string[] arrFolder = this.textBox1.Text.Split('\\');
            string Path = this.textBox2.Text + '\\' + arrFolder[arrFolder.Length - 1];
            if (!Directory.Exists(Path))
            {
                this.CopyFolder(this.textBox1.Text, Path);
                MessageBox.Show("复制成功");
            }
            else
            {
                DialogResult dr = MessageBox.Show("目标文件夹已存在，是否覆盖", "提示: ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    //强制删除目标文件夹以及子文件夹
                   Directory.Delete(Path,true);
                   this.CopyFolder(this.textBox1.Text, Path);
                    MessageBox.Show("复制成功");
                }
                return;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (!(Directory.Exists(textBox1.Text) && Directory.Exists(this.textBox2.Text)))
            {
                MessageBox.Show("文件夹不存在");
                return;
            }
            string[] arrFolder = this.textBox1.Text.Split('\\');
            string Path = this.textBox2.Text + '\\' + arrFolder[arrFolder.Length - 1];
           

            if (!Directory.Exists(Path))
            {
                DirectoryInfo directoryInfo2 = new DirectoryInfo(this.textBox1.Text);
                directoryInfo2.MoveTo(Path);
                MessageBox.Show("移动成功");
            }
            else
            {
                DialogResult dr = MessageBox.Show("目标文件夹已存在，是否覆盖", "提示: ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    DirectoryInfo directoryInfo2 = new DirectoryInfo(this.textBox1.Text);
                    //强制删除目标文件夹以及子文件夹
                    Directory.Delete(Path, true);
                    directoryInfo2.MoveTo(Path);
                    MessageBox.Show("移动成功");
                }
                return;
            }

        
        }
        //编写通用的递归方法 ,实现文件夹复制
        /// <summary>
        /// 
        /// </summary>
        /// <param name="startFolder">原始文件夹路径</param>
        /// <param name="endFolder">目标文件夹路径</param>
        /// endFolder 是处理好后的路径
        private void CopyFolder(string startFolder, string endFolder)
        {
            //创建文件夹
            Directory.CreateDirectory(endFolder);
            //获取原始文件夹下的所有文件
            DirectoryInfo directoryInfo = new DirectoryInfo(startFolder);

            foreach (FileInfo item in directoryInfo.GetFiles())
            {
                item.CopyTo(endFolder + '\\' + item.Name);
            }
            //循环所有子目录形成递归调用
            foreach (DirectoryInfo item in directoryInfo.GetDirectories())
            {
                //获取当前递归层级文件名
                string startPath = item.FullName;
                //要递归的子级目录路径
                string endPath = endFolder + '\\' + item.Name;
                CopyFolder(startPath, endPath);
            }
        }


    }
}
